package com.lunwen.view;

import com.lunwen.dao.StudentDao;
import com.lunwen.domain.Student;
import com.lunwen.view.component.Context;
import com.lunwen.view.component.MassageTip;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;

public class SearchStudentFrame extends Context implements ActionListener, KeyListener {


    public JTextField search = new JTextField(10);
    public JTextField sid = new JTextField(10);
    public JTextField name = new JTextField(10);
    public JTextField major = new JTextField(10);
    public JTextField clazz = new JTextField(10);
    public JTextField professor = new JTextField(10);
    public JTextField phone = new JTextField(10);

    private JButton left = new JButton("<");
    private JButton right = new JButton(">");

    private StudentDao dao = new StudentDao();
    private List<Student> cache = null;
    private int indexOfCache = 0;

    public SearchStudentFrame(int width, int height) {
        super(width, height);

        search.setBounds(60,0,width-120,40);

        sid.setBounds(60,50,width-120,40);
        name.setBounds(60,100,width-120,40);
        major.setBounds(60,150,width-120,40);
        clazz.setBounds(60,200,width-120,40);
        professor.setBounds(60,250,width-120,40);
        phone.setBounds(60,300,width-120,40);

        this.add(search, "搜索", 100);
        this.add(sid, "学生学号", 100);
        this.add(name, "学生姓名", 100);
        this.add(major, "专业名称", 100);
        this.add(clazz, "所在班级", 100);
        this.add(professor, "导师姓名", 100);
        this.add(phone, "学生联系方式", 150);

        left.setBounds(0,(height-40)/2,40,40);
        this.add(left,20);
        right.setBounds(width-40,(height-40)/2,40,40);
        this.add(right,20);

        search.addKeyListener(this);
        left.addActionListener(this);
        right.addActionListener(this);
        search.setText("按照名称搜索，回车确认");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (cache != null){
            if (e.getSource() == left){
                if (indexOfCache > 0){
                    indexOfCache --;
                    setValue(cache.get(indexOfCache));
                }else{
                    MassageTip.showMassage(null,"前面再没有了");
                }
            }else if (e.getSource() == right){
                if (indexOfCache < cache.size()-1){
                    indexOfCache ++;
                    setValue(cache.get(indexOfCache));
                }else {
                    MassageTip.showMassage(null,"后面再没有了");
                }
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyChar() == KeyEvent.VK_ENTER){
            cache = dao.find(search.getText());
            indexOfCache = 0;
            if (cache != null && cache.size() != 0){
                setValue(cache.get(indexOfCache));
                MassageTip.showMassage(null,"找到"+cache.size()+"个");
            }else {
                MassageTip.showMassage(null,"没有找到");
            }
        }
    }

    private void setValue(Student student) {
        sid.setText(student.getSid());
        name.setText(student.getName());
        major.setText(student.getMajor());
        clazz.setText(student.getClazz());
        professor.setText(student.getProfessor());
        phone.setText(student.getPhone());
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}
